from collections import defaultdict
anno1 = open("/home/zyx/Downloads/res_combine5_3.csv","rt").readlines()[1:]
anno1 = map(lambda x:x.strip().split(","), anno1)
ann1_dict = {}
for anno in anno1:
    filename, x0, y0,x1,y1 = anno[:5]
    if float(x1) - float(x0) < 50:
        continue

    try:
        ann1_dict[filename].append([float(x0),float(y0),float(x1),float(y1), 1])
    except KeyError:
        ann1_dict[filename] = []
        ann1_dict[filename].append([float(x0),float(y0),float(x1),float(y1), 1])

anno2 = open("/data1/zyx/yks/cvutils/output/retail_deformable_multi_scales_flipped_1538476933.55.txt","rt").readlines()[1:]
anno2 = map(lambda x:x.strip().split("\t"), anno2)
ann2_dict = {}
import os
for anno in anno2:
    filename, x0, y0,x1,y1, score = anno[:6]
    if float(x1) - float(x0) < 50:
        continue
    filename = os.path.basename(filename)
    if float(score) > .97:
        try:
            ann2_dict[filename].append([float(x0),float(y0),float(x1),float(y1), 1])
        except KeyError:
            ann2_dict[filename] = []
            ann2_dict[filename].append([float(x0),float(y0),float(x1),float(y1), 1])

filenames1 = set(ann1_dict.keys())
filenames2 = set(ann2_dict.keys())
print(len(filenames1 & filenames2), len(filenames1), len(filenames2))
print(filenames2 - filenames1)
from gluoncv.utils.viz import plot_bbox
import matplotlib.pyplot as plt
import cv2,os
import numpy as np
img_r = "/data1/zyx/yks/dataset/retail/test_images/test"

count0 = 0
count1 = 0

for key in filenames1 & filenames2:
    count0 += len(ann1_dict[key])
    count1 += len(ann2_dict[key])
print(count0,count1)

for key in filenames1 & filenames2:
    ax = plt.subplot(121)
    bboxes = np.array(ann2_dict[key])
    plot_bbox(cv2.imread(os.path.join(img_r,key))[:,:,::-1],
              bboxes=bboxes[:,:4],
              ax = ax
              )
    ax = plt.subplot(122)
    bboxes = np.array(ann1_dict[key])
    plot_bbox(cv2.imread(os.path.join(img_r,key))[:,:,::-1],
              bboxes=bboxes[:,:4],
              ax = ax
              )
    print(key)
    plt.show()